#!/usr/bin/sh
set -e

distribution=$(cat /etc/*-release|grep "^ID="|tr -d "ID=")

TZ="Asia/Shanghai"
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '${TZ}' > /etc/timezone


case "$distribution" in
    "debian")
        cp /etc/apt/sources.list /etc/apt/sources.list_bak
        apt-get update && apt-get install -y  ca-certificates apt-transport-https
        sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
        # sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
        package_list="apt-utils \
            openssh-client \
            gnupg2 \
            dirmngr \
            iproute2 \
            procps \
            lsof \
            htop \
            net-tools \
            psmisc \
            rsync \
            ca-certificates \
            unzip \
            zip \
            less \
            jq \
            lsb-release \
            apt-transport-https \
            dialog \
            libc6 \
            libgcc1 \
            libkrb5-3 \
            libgssapi-krb5-2 \
            libicu[0-9][0-9] \
            liblttng-ust[0-9] \
            libstdc++6 \
            zlib1g \
            locales \
            sudo \
            ncdu \
            man-db \
            strace \
            manpages \
            manpages-dev \
            init-system-helpers \
            zsh csh tcsh fish bash tmux \
            git openssh-server vim neovim emacs nano \
            autojump fzf nnn ranger \
            curl wget axel aria2
        "
         # Install libssl1.1 if available
        if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
            package_list="${package_list}  libssl1.1"
        fi

        # Install appropriate version of libssl1.0.x if available
        libssl_package=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
        if [ "$(echo "$LIlibssl_packageBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
            if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
                # Debian 9
                package_list="${package_list} libssl1.0.2"
            elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
                # Ubuntu 18.04, 16.04, earlier
                package_list="${package_list} libssl1.0.0"
            fi
        fi
        echo "Packages to verify are installed: ${package_list}"
        apt-get update && apt-get -y install --no-install-recommends ${package_list}
        # docker 报错bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8) 解决方案
        apt-get clean && apt-get update && apt-get install -y locales && locale-gen en_US.UTF-8
    ;;
    "ubuntu")
        cp /etc/apt/sources.list /etc/apt/sources.list_bak
        apt-get update && apt-get install -y  ca-certificates apt-transport-https
        sed -i "s@http://.*archive.ubuntu.com@https://mirrors.ustc.edu.cn@g" /etc/apt/sources.list
        sed -i "s@http://.*security.ubuntu.com@https://mirrors.ustc.edu.cn@g" /etc/apt/sources.list
        # sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
        # sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
        package_list="apt-utils \
            openssh-client \
            gnupg2 \
            dirmngr \
            iproute2 \
            procps \
            lsof \
            htop \
            net-tools \
            psmisc \
            rsync \
            ca-certificates \
            unzip \
            zip \
            less \
            jq \
            lsb-release \
            apt-transport-https \
            dialog \
            libc6 \
            libgcc1 \
            libkrb5-3 \
            libgssapi-krb5-2 \
            libicu[0-9][0-9] \
            liblttng-ust[0-9] \
            libstdc++6 \
            zlib1g \
            locales \
            sudo \
            ncdu \
            man-db \
            strace \
            manpages \
            manpages-dev \
            init-system-helpers \
            zsh csh tcsh fish bash tmux \
            git openssh-server vim neovim emacs nano \
            autojump fzf nnn ranger \
            curl wget axel aria2
        "
         # Install libssl1.1 if available
        if [[ ! -z $(apt-cache --names-only search ^libssl1.1$) ]]; then
            package_list="${package_list}  libssl1.1"
        fi

        # Install appropriate version of libssl1.0.x if available
        libssl_package=$(dpkg-query -f '${db:Status-Abbrev}\t${binary:Package}\n' -W 'libssl1\.0\.?' 2>&1 || echo '')
        if [ "$(echo "$LIlibssl_packageBSSL" | grep -o 'libssl1\.0\.[0-9]:' | uniq | sort | wc -l)" -eq 0 ]; then
            if [[ ! -z $(apt-cache --names-only search ^libssl1.0.2$) ]]; then
                # Debian 9
                package_list="${package_list} libssl1.0.2"
            elif [[ ! -z $(apt-cache --names-only search ^libssl1.0.0$) ]]; then
                # Ubuntu 18.04, 16.04, earlier
                package_list="${package_list} libssl1.0.0"
            fi
        fi
        echo "Packages to verify are installed: ${package_list}"
        apt-get update && apt-get -y install --no-install-recommends ${package_list}
        # docker 报错bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf-8) 解决方案
        apt-get clean && apt-get update && apt-get install -y locales && locale-gen en_US.UTF-8
    ;;
    "alpine"*)
        cp /etc/apk/repositories /etc/apk/repositories.bak
        sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
        # sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
        package_list="openssh-client \
            gnupg \
            procps \
            lsof \
            htop \
            net-tools \
            psmisc \
            rsync \
            ca-certificates \
            unzip \
            zip \
            less \
            jq \
            libgcc \
            libstdc++ \
            krb5-libs \
            libintl \
            libssl1.1 \
            lttng-ust \
            tzdata \
            userspace-rcu \
            zlib \
            sudo \
            coreutils \
            sed \
            grep \
            which \
            ncdu \
            shadow \
            strace \
            git bash zsh tmux fish tcsh openssh-server net-tools vim nano neovim emacs \
            curl wget fzf nnn ranger \
            perl ruby
        "
        echo "Packages to verify are installed: ${package_list}"
        apk update && apk add --no-cache ${package_list}
    ;;
    "darwin"*)
        :"macos"
    ;;
    "cygwin" | "msys" | "win32")
        : "Windows"
    ;;
    *)
        printf '%s\n' "Unknown OS detected, aborting..." >&2
        exit 1
    ;;
esac

    
# Install git if not already installed (may be more recent than distro version)
# if ! type git > /dev/null 2>&1; then
#     apt-get -y install --no-install-recommends git
# fi












